﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Data
{
    public class ServiceDao : IServiceDao
    {
        public List<Service> GetAllServices()
        {
            List<Service> services;

            using (var context = new MonkeyWarrantyEntities())
            {
                services = context.Services.OrderBy(x => x.Name).ToList();
            }

            return services;
        }

        public Service AddService(Service service)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.Services.AddObject(service);
                context.SaveChanges();
            }

            return service;
        }

        public void UpdateService(Service service)
        {
            // throw new System.NotImplementedException();
            if (service == null || service.Id == 0) return;

            using (var context = new MonkeyWarrantyEntities())
            {
                context.Services.Attach(service);
                context.ObjectStateManager.ChangeObjectState(service, EntityState.Modified);
                context.SaveChanges();
            }
        }

        public void DeleteService(int serviceId)
        {
            if (serviceId <= 0) return;
            using (var context = new MonkeyWarrantyEntities())
            {
                var service = context.Services.First(g => g.Id == serviceId);

                context.DeleteObject(service);

                context.SaveChanges();
            }
        }

        public int GetMaxId()
        {
            var maxId = 0;

            using (var context = new MonkeyWarrantyEntities())
            {
                var service = context.Services.OrderByDescending(x => x.Id).FirstOrDefault();
                if (service != null) maxId = service.Id;
            }

            return maxId;
        }

        public int GetCurrentIdentity()
        {
            long? identity;
            using (var context = new MonkeyWarrantyEntities())
            {
                identity = context.GetIdentity("Services").SingleOrDefault();
            }

            return identity.HasValue ? (int)identity.Value : 0;
        }

        public Service GetById(int id)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.Services.FirstOrDefault(x => x.Id == id);
            }
        }

        public List<Service> GetServices(string name, int startIndex, int pageSize, string sorting)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                IEnumerable<Service> query = db.Services;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                //Sorting
                //This ugly code is used just for demonstration.
                //Normally, Incoming sorting text can be directly appended to an SQL query.
                if (string.IsNullOrEmpty(sorting) || sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(x => x.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(x => x.Name);
                }
                else if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }

                return pageSize > 0
                           ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                           : query.ToList(); //No paging
            }
        }

        public int GetServiceCount(string name)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                IEnumerable<Service> query = db.Services;

                //Filters
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                return query.Count();
            }
        }
    }
}
